<template>
  <a-modal
    :title="'采集器'+gatewayName+'的参数配置'"
    :width="1320"
    :visible="visible"
    :footer="null"
    @cancel="handleCancel"
  >
    <a-card :bordered="false">

      <div class="table-operator" style="text-align: right;padding-right: 10px;">
        <a-button type="primary" class="btn_add" @click="$refs.selectForm.add(gatewayId)" icon="plus">新增</a-button>
      </div>

      <s-table
        ref="table"
        size="middle"
        :columns="columns"
        :data="loadData"
        :alert="true"
        :rowKey="(record) => record.id"
      >
        <span slot="status" slot-scope="text">
          {{ text == 0? '禁用':'启用' }}
        </span>

        <span slot="action" slot-scope="text, record">
          <a @click="$refs.editForm.edit(record)" class="btn_edit">编辑</a>
          <a-divider type="vertical"/>
          <a-popconfirm class="btn_del" placement="topRight" title="确认删除？" @confirm="() => del(record)">
            <a>删除</a>
          </a-popconfirm>
<!--          <a-dropdown>-->
<!--            <a class="btn_more"> 更多 <a-icon type="down"/></a>-->
<!--            <a-menu slot="overlay">-->
<!--              <a-menu-item>-->
<!--                <a-popconfirm placement="topRight" title="确认删除？" @confirm="() => del(record)">-->
<!--                  <a>删除</a>-->
<!--                </a-popconfirm>-->
<!--              </a-menu-item>-->
<!--            </a-menu>-->
<!--          </a-dropdown>-->
        </span>
      </s-table>

      <add-form ref="addForm" @ok="handleOk"/>
      <edit-form ref="editForm" @ok="handleOk"/>
      <select-form ref="selectForm" @ok="handleOk"/>
    </a-card>
  </a-modal>
</template>

<script>
  import { STable } from '@/components'
  import { page, del } from '@/api/modular/main/bizcollectorconfig/bizCollectorConfigManage'
  import addForm from './addForm'
  import editForm from './editForm'
  import selectForm from './selectForm'

  export default {
    components: {
      STable,
      addForm,
      editForm,
      selectForm
    },

    data () {
      return {
        advanced: false, // 高级搜索 展开/关闭
        queryParam: {}, // 查询参数
        columns: [ // 表头
          {
            title: '名称',
            dataIndex: 'configName',
            width: 200,
            ellipsis: true
          },
          {
            title: '编码',
            dataIndex: 'code'
          },
          {
            title: '值',
            dataIndex: 'value'
          },
          {
            title: '状态',
            dataIndex: 'status',
            scopedSlots: { customRender: 'status' }
          },
          {
            title: '备注',
            dataIndex: 'remark',
            width: 200,
            ellipsis: true
          },
          {
            title: '操作',
            dataIndex: 'action',
            width: '150px',
            scopedSlots: { customRender: 'action' }
          }
        ],
        // 加载数据方法 必须为 Promise 对象
        loadData: parameter => {
          return page(Object.assign(parameter, this.queryParam)).then((res) => {
            return res.data
          })
        },
        selectedRowKeys: [],
        selectedRows: [],
        defaultExpandedKeys: [],
        replaceFields: {
          key: 'id'
        },
        visible: false,
        gatewayId: 0,
        gatewayName: ''
      }
    },
    methods: {
      toIndex (record) {
        this.visible = true
        this.gatewayId = record.id
        this.gatewayName = record.gatewayName
        this.queryParam.gatewayId = record.id
        try {
          this.$refs.table.refresh()
        } catch (e) {
          // 首次进入界面，因表格加载顺序，会抛异常，我们不予理会
        }
      },
      del (record) {
        del(record).then((res) => {
          if (res.success) {
            this.$message.success('删除成功')
            this.$refs.table.refresh()
          } else {
            this.$message.error('删除失败：' + res.message)
          }
        }).catch((err) => {
          this.$message.error('删除错误：' + err.message)
        })
      },
      toggleAdvanced () {
        this.advanced = !this.advanced
      },
      handleOk () {
        this.$refs.table.refresh()
      },
      onSelectChange (selectedRowKeys, selectedRows) {
        this.selectedRowKeys = selectedRowKeys
        this.selectedRows = selectedRows
      },
      handleCancel () {
        this.queryParam = {}
        this.visible = false
      }
    }
  }
</script>

<style lang="less">
  .table-operator {
    margin-bottom: 18px;
  }

  button {
    margin-right: 8px;
  }
</style>
